package com.zjpavt.client.extenddevice.helpbean;

import com.zjpavt.client.extenddevice.IExtendDevice;
import lombok.extern.slf4j.Slf4j;

/**
 * 下位机交互状态统计类
 * 设计主要是为了统计回复指令的正确性
 * 主要分为 确认设备指令 交互指令两种
 * 每种指令统计 1.确认正常次数 2.确认异常次数 3.连续异常次数
 * 一般连续异常3次发起设备的重新确认 如果无反馈则断开设备连接
 * 根据设备类型重排LoopManager ElecManager ExtendDeviceManager
 * @author zyc
 */
@Slf4j
public class UnderDeviceInteractionBean {
    /**
     * 告警阈值
     */
    private int alarmThreshold = 3;
    public UnderDeviceInteractionBean(IExtendDevice owner) {
        this.owner = owner;
    }

    public UnderDeviceInteractionBean(int alarmThreshold, IExtendDevice owner) {
        this.alarmThreshold = alarmThreshold;
        this.owner = owner;
    }

    private IExtendDevice owner;
    /**
     * 确认成功次数
     */
    private int checkSuccessTimes;
    /**
     * 确认失败次数
     */
    private int checkFailTimes;
    /**
     * 连续确认失败次数
     */
    private int continuousCheckFailTimes;
    /**
     * 交互成功次数
     */
    private int interactionSuccessTimes;
    /**
     * 交互失败次数
     */
    private int interactionFailTimes;
    /**
     * 连续交互失败次数
     */
    private int continuousInteractionFailTimes;

    public void checkSuccess() {
        checkSuccessTimes++;
        continuousCheckFailTimes = 0;
        continuousInteractionFailTimes = 0;
    }

    public void checkFail() {
        checkFailTimes++;
        continuousCheckFailTimes++;
        String failCountInformation = owner.getDeviceType().getDeviceName() + " in extend mouth" + owner.getExtendNum()
                                      + "check success times=" + checkSuccessTimes + " fail times=" + checkFailTimes;
        log.info(failCountInformation );
        if (continuousCheckFailTimes > alarmThreshold) {
            log.error("continuousCheckFailTimes bigger than alarmThreshold, device disconnected");
            owner.disConnected();
        }
    }

    public void interactionSuccess() {
        interactionSuccessTimes++;
        continuousInteractionFailTimes = 0;
    }

    public void interactionFail() {
        interactionFailTimes++;
        continuousInteractionFailTimes++;
        String failCountInformation = owner.getDeviceType().getDeviceName() + " in extend mouth" + owner.getExtendNum()
                                      + "interaction success times=" + interactionSuccessTimes + " fail times=" + interactionFailTimes;
        log.info(failCountInformation);
        if (continuousInteractionFailTimes > alarmThreshold) {
            log.error("continuousInteractionFailTimes bigger than alarmThreshold, device reCheck");
            owner.reCheck();
        }
    }


    public int getCheckSuccessTimes() {
        return checkSuccessTimes;
    }

    public int getCheckFailTimes() {
        return checkFailTimes;
    }

    public int getContinuousCheckFailTimes() {
        return continuousCheckFailTimes;
    }

    public int getInteractionSuccessTimes() {
        return interactionSuccessTimes;
    }

    public int getInteractionFailTimes() {
        return interactionFailTimes;
    }

    public int getContinuousInteractionFailTimes() {
        return continuousInteractionFailTimes;
    }
}
